package jgl.vajra.operator.part.mine.activity;

import java.util.HashMap;
import java.util.Map;

import jgl.vajra.operator.R;
import jgl.vajra.operator.bean.InvitePrizeBalanceBean;
import jgl.vajra.operator.bean.InvitePrizeBean;
import jgl.vajra.operator.databinding.ActivityInvitePrizeBinding;
import jgl.vajra.operator.dialogFragment.ShareFriendsDialogFragment;
import jgl.vajra.operator.manager.AccountManager;
import jgl.vajra.operator.manager.IntentManager;
import jgl.vajra.operator.part.mine.contract.InvitePrizeContract;
import jgl.vajra.operator.part.mine.viewModel.InvitePrizeViewModel;
import jgl.vajra.operator.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.operator.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.operator.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.operator.widget.utils.ToastUtils;
import jgl.vajra.operator.widget.utils.UIUtils;

@CreateViewModel(InvitePrizeViewModel.class)
public class InvitePrizeActivity extends BaseMVVMActivity<InvitePrizeViewModel, ActivityInvitePrizeBinding> implements InvitePrizeContract.View {

    private ShareFriendsDialogFragment shareFriendsDialogFragment;
    private InvitePrizeBean dataBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_invite_prize;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);
        dataBean = new InvitePrizeBean();

        requestNetData();
    }

    private void requestNetData() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        mViewModel.getInvitePrizeBalanceData(map);

        mViewModel.getInvitePrizeOwnerData(map);

        mViewModel.getInvitePrizeOperatorData(map);

        mViewModel.getInvitePrizeSumData(map);

    }

    //活动规则
    @Override
    public void onRule() {
        IntentManager.toInviteRuleActivity(this);
    }

    //点击邀请
    @Override
    public void onInvite() {
        if (shareFriendsDialogFragment == null)
            shareFriendsDialogFragment = new ShareFriendsDialogFragment();
        shareFriendsDialogFragment.setShareData(this);
        shareFriendsDialogFragment.show(this.getSupportFragmentManager());
    }

    //提现到钱包
    @Override
    public void onWithdrawal(int amount, int type) {
        if (amount == 0) {
            ToastUtils.showShort("提现金额不能小于等于0");
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        map.put("amount", AEACCoder.encrypt(amount + ""));
        map.put("tag", 2);
        map.put("type", type);
        mViewModel.getWithdrawalBalanceData(map);
    }

    @Override
    public void returnInvitePrizeBalanceData(InvitePrizeBalanceBean data) {
        dataBean.downloads = data.downloads;
        dataBean.firstOrderNumber = data.firstOrderNumber;
        dataBean.sumRebate = data.sumRebate;
        dataBean.withdraw = data.withdraw;
        dataBean.inviteBalance = data.inviteBalance;

        mBinding.setData(dataBean);
    }

    @Override
    public void returnWithdrawalBalanceData() {
    }

    @Override
    public void returnInvitePrizeOwnerData(InvitePrizeBalanceBean data) {
        dataBean.downloadsOwner = data.downloads;
        dataBean.certificateCount = data.certificateCount;
        dataBean.publicizeCount = data.publicizeCount;
        dataBean.certificateRebate = data.certificateRebate;
        dataBean.publicizeRebate = data.publicizeRebate;
        dataBean.balance = data.balance;

        mBinding.setData(dataBean);
    }

    @Override
    public void returnInvitePrizeOperatorData(InvitePrizeBalanceBean data) {

        dataBean.downloadsOperator = data.downloads;
        dataBean.certificateCountOperator = data.certificateCount;
        dataBean.certificateRebateOperator = data.certificateRebate;
        dataBean.withdrawOperator = data.withdraw;
        dataBean.balanceOperator = data.balance;

        mBinding.setData(dataBean);
    }

    @Override
    public void returnInvitePrizeSumData(InvitePrizeBalanceBean data) {
        dataBean.firstRebate = data.firstRebate;
        dataBean.firstUseDay = data.firstUseDay;
        dataBean.rebate = data.rebate;
        dataBean.grade = data.grade;
        dataBean.useDay = data.useDay;
        dataBean.balanceSum = data.balance;

        dataBean.firstData = "目前最高排名奖励达到" + data.firstRebate + "元，用时" + data.firstUseDay + "天";

        mBinding.setData(dataBean);
    }
}
